package com.wewins.fota.bean.bo.excel.column;

import com.wewins.fota.bean.bo.excel.CellSource;
import com.wewins.fota.bean.bo.excel.Column;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.Date;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TimeColumn implements Column {
    private Object value;

    @Builder.Default
    private CellType cellType = CellType.NUMERIC;

    @Override
    public void fillCell(CellSource source) {
        Workbook wb = source.getWorkbook();
        Cell cell = source.getCell();
        long time = (Long) value;
        if (time <= 0) {
            cell.setCellValue("-");
        } else {
            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setDataFormat(wb.createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
            cell.setCellStyle(cellStyle);
            cell.setCellValue(new Date(time));
        }
    }
}
